home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9104.ARJ / 9N04059A < prev    next >
Text File  |  1991-02-14  |  6KB  |  176 lines

  1.  
  2. /**********************************************************
  3.     An implementation of the point and shoot menu system.
  4.     The main function is a simple demonstration routine.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <conio.h>
  9.  
  10. #define CRET     0x0d           /* key definitions */
  11. #define TAB      0x09
  12. #define BACKTAB  0x0f           /* key scan codes */
  13. #define UPARROW  0x48
  14. #define DNARROW  0x50
  15. #define LTARROW  0x4b
  16. #define RTARROW  0x4d
  17. #define PAGEUP   0x49
  18. #define PAGEDN   0x51
  19. #define HOMEKEY  0x47
  20. #define ENDKEY   0x4f
  21. #define CTRLEND  0x75
  22.  
  23.  
  24. /**********************************************************
  25.     display one line of menu at the proper location.  If
  26.     an X offset is specified then this must be horizontal
  27.     formatted menu.
  28. */
  29. void show (int x,int y,int xoff,int yoff,char *p,int fore)
  30. {
  31.     if (xoff)                 /* if an X offset exist */
  32.         x += xoff;
  33.     else
  34.         y += yoff;
  35.  
  36.     gotoxy(x,y);
  37.     textcolor(fore|8);        /* use intense color */
  38.     putch(*p++);              /* print the first char */
  39.     textcolor(fore);          /* back to normal color */
  40.     cputs(p);                 /* print the rest of it */
  41. }
  42.  
  43. /**********************************************************
  44.     search the p list for a beginning character in the
  45.     menu list that matches c.
  46. */
  47. int findletter (char c, char *p[], int i)
  48. {
  49.     int j = 0;
  50.  
  51.     while (*p[j] != NULL)      /* while not at end */
  52.     if (c == *p[j])            /* if we find a match */
  53.         { i = j; break; }      /* record and quit */
  54.     else
  55.         j++;
  56.  
  57.     return(i);                 /* return the new idx */
  58. }
  59.  
  60. /**********************************************************
  61.     initialize certain location variables to allow us
  62.     to write at proper screen location.
  63. */
  64. int init (char *p[], int horz, int *xoff)
  65. {
  66.     int max = 0, x = 0;
  67.  
  68.     if (horz)                      /* if horizontal format */
  69.     {
  70.         while (p[max] != NULL)     /* while not at end */
  71.         {
  72.             *xoff++ = x;           /* calc X offsets */
  73.             x += strlen(p[max++]); /* add length of item */
  74.         }
  75.     }
  76.     else                           /* else zero it out */
  77.     {
  78.         while (p[max++] != NULL) *xoff++ = 0;
  79.         max--;
  80.     }
  81.  
  82.     return(max);                   /* number of items found */
  83. }
  84.  
  85. /**********************************************************
  86.     set the index according to user's response.  returns
  87.     the new index value or -1 if the user pressed enter.
  88. */
  89. int keypress (char *p[], char c, int i, int max)
  90. {
  91.     int done = 0;
  92.  
  93.     switch(c)                   /* switch on user input */
  94.     {
  95.         case UPARROW:
  96.         case LTARROW:
  97.         case BACKTAB:   i--;            break;
  98.         case DNARROW:
  99.         case RTARROW:
  100.         case TAB:       i++;            break;
  101.         case CRET:      done = 1;       break;
  102.         default:        i = findletter(c,p,i);
  103.     }
  104.  
  105.     if (done) i = -1; else      /* if done, ret -1 */
  106.     if (i < 0) i = max-1; else  /* else do range check */
  107.     if (i == max) i = 0;
  108.  
  109.     return(i);                  /* ret new index */
  110. }
  111.  
  112. /**********************************************************
  113.     display a point and shoot menu.  Return the first
  114.     character of the menu option selected.
  115. */
  116. char menu (char *p[], int horz)
  117. #define MAX_ELEMENTS 12
  118. {
  119.     struct text_info info;      /* holds text screen info */
  120.     int i, j, x = 0, y, fore, back, max, savei;
  121.     int xoff[MAX_ELEMENTS];     /* for use w/ horiz format */
  122.     char c;
  123.  
  124.     max = init(p,horz,xoff);    /* calc screen positions */
  125.     gettextinfo(&info);         /* get current screen attr */
  126.     fore = info.attribute & 0x0f;/* extract foreground color */
  127.     back = info.attribute >> 4; /* extract background color */
  128.     x = info.curx;              /* establish reference location */
  129.     y = info.cury;
  130.     _setcursortype(_NOCURSOR);  /* turn cursor off */
  131.     for (i=0; i<max; i++)       /* display all menu items */
  132.         show(x,y,xoff[i],i,p[i],fore);
  133.     i = 0;
  134.  
  135.     do
  136.     {
  137.         textcolor(back);                /* set reverse video mode */
  138.         textbackground(fore);
  139.         show(x,y,xoff[i],i,p[i],back);  /* display selected item */
  140.         if ((c = toupper(getch())) == 0)/* if 0 is returned */
  141.             c = getch();                /* then get scan code */
  142.         textcolor(fore);                /* set normal video mode */
  143.         textbackground(back);
  144.         show(x,y,xoff[i],i,p[i],fore);  /* redisplay item */
  145.         savei = i;                      /* save prev sel item */
  146.     }
  147.     while ((i = keypress(p,c,i,max)) != -1); /* quit when user selects */
  148.  
  149.     _setcursortype(_NORMALCURSOR);      /* turn cursor back on */
  150.     return(*p[savei]);                  /* return selection */
  151. }
  152.  
  153. /**********************************************************
  154.     demonstration main function to show use of the point
  155.     and shoot menu.
  156. */
  157. void main (void)
  158. {
  159.     char *p[] = {"First  ","Second  ","Third  ",
  160.                  "Quit  ",NULL}, c;
  161.     int format = 0;
  162.  
  163.     textcolor(LIGHTGRAY);
  164.     textbackground(BLUE);
  165.     clrscr();
  166.     cputs("Display menu in horizontal format? (y/n) >");
  167.     if (toupper(getch()) == 'Y') format = 1;
  168.     gotoxy(10,10);
  169.     while ((c = menu(p,format)) != 'Q')
  170.     {
  171.         gotoxy(10,20);
  172.         cprintf("The selected option is : %c\n\r",c);
  173.         gotoxy(10,10);
  174.     }
  175. }
  176.